﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace BetResult
{
    public partial class frmMain : Form
    {
        private UrlType urlType = UrlType.utNone;
        private string urlBaidu = "http://www.lecai.com/lottery/draw/view/557";
        private string url168 = "http://www.168kai.com/Lottery/10016";
        private string urlPk10 = "http://www.pk10.com/kaijiang.asp";

        private string resultPeroid = string.Empty;
        private string resultValue = string.Empty;


        private string oldPeroid = string.Empty;

        private int Count = 0;
        private int siteCount = 0;
        private int totalSites = 3;

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Interval = (int)numInterval.Value * 1000;
            timer1.Enabled = true;

            btnStart.Enabled = false;
        }

        private void webResult_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webResult.ReadyState != WebBrowserReadyState.Complete)
                return;

            timer2.Enabled = true;

        }

        private void LogText2(string text)
        {
            txtLog.AppendText(text + Environment.NewLine);
            if (txtLog.Lines.Length > 1000)
                txtLog.Clear();
        }
        private void LogText(string peroid, string result)
        {
            txtLog.AppendText(DateTime.Now.ToString() + Environment.NewLine);
            txtLog.AppendText("当前期数：" + peroid + Environment.NewLine);
            txtLog.AppendText("中奖号码：" + result + Environment.NewLine);

            if (txtLog.Lines.Length > 1000)
                txtLog.Clear();
        }

        private void GetPK10Result(WebBrowser web, ref string peroid, ref string result)
        {
            if (web.Document == null)
                return;

            HtmlElementCollection eles = web.Document.GetElementsByTagName("strong");
            HtmlElement myele;
            try
            {
                foreach (HtmlElement ele in eles)
                {
                    if (ele.GetAttribute("classname").Trim() == "dqq")
                    {
                        peroid = ele.InnerText;
                        if (!string.IsNullOrEmpty(peroid))
                            peroid = peroid.Trim();
                        break;
                    }
                }

                myele = web.Document.GetElementById("jc");
                if (myele == null)
                    return;

                if (myele.Children[0] == null)
                    return;

                string num = string.Empty;
                for (int i = 0; i < 10; i++)
                {
                    num = num + myele.Children[i].InnerText.Trim().PadLeft(2, '0') + ";";
                }
                result = num;

            }
            catch(Exception ex)
            {
                LogText2(ex.Message);
            }
        }
        private void Get168Result2(WebBrowser web, ref string peroid, ref string result)
        {
            if (web.Document == null)
                return;

            try
            {
                HtmlElement ele = web.Document.GetElementById("cterm");
                if (ele == null)
                    return;

                peroid = ele.InnerText;
                if (!string.IsNullOrEmpty(peroid))
                {
                    peroid = peroid.Trim();
                    if (peroid == "000000")
                        return;
                }

                ele = web.Document.GetElementById("term_ball");
                if (ele.Children[0] == null)
                    return;

                string num = string.Empty;
                for (int i = 0; i < 10; i ++ )
                {
                    num = num + ele.Children[i].GetAttribute("title").Trim().PadLeft(2, '0') + ";";
                }
                result = num;
            }
            catch (Exception ex)
            {
                LogText2(ex.Message);
            }

        }
        private void Get168Result(WebBrowser web, ref string peroid, ref string result)
        {
            if (web.Document == null)
                return;

            HtmlElementCollection eles = web.Document.GetElementsByTagName("div");
            try
            {
                foreach (HtmlElement ele in eles)
                {
                    HtmlElement myele;
                    if (ele.GetAttribute("classname").Trim() == "item items_2  info_10016")
                    {
                        myele = ele.Children[0];
                        if (myele != null)
                        {
                            myele = myele.Children[1];
                            if (myele != null)
                            {
                                myele = myele.Children[0];
                                if (myele != null)
                                {
                                    peroid = myele.InnerText;
                                    if (!string.IsNullOrEmpty(peroid))
                                    {
                                        peroid = peroid.Trim();
                                        if (peroid == "000")
                                            return;
                                    }
                                }
                            }
                        }

                        myele = ele.Children[2];
                        if (myele != null)
                        {
                            myele = myele.Children[0];
                            if (myele != null)
                            {
                                myele = myele.Children[0];
                                if (myele != null)
                                {
                                    myele = myele.Children[0];
                                    if (myele != null)
                                    {
                                        result = myele.GetAttribute("title");
                                        if (!string.IsNullOrEmpty(result))
                                            result = result.Trim().PadLeft(2, '0');
                                    }
                                }
                            }
                        }
                        return;
                    }
                }
            }
            catch(Exception ex)
            {
                LogText2(ex.Message);
            }

        }


        private void GetBaiDuResult(WebBrowser web, ref string peroid, ref string result)
        {
            if (web.Document == null)
                return;

            HtmlElement ele = web.Document.GetElementById("jq_latest_draw_phase");
            if (ele == null)
                return;

            try
            {
                peroid = ele.InnerText.Trim();

                ele = web.Document.GetElementById("jq_latest_draw_result");
                if (ele == null)
                    return;

                
                if (ele.Children[0] == null)
                    return;
                string num = string.Empty;
                for (int i = 0; i < 10; i ++)
                {
                    num = num + ele.Children[i].InnerText.Trim() + ";";
                }
                result = num;
            }
            catch(Exception ex)
            {
                LogText2(ex.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            resultPeroid = string.Empty;
            resultValue = string.Empty;
            switch (urlType)
            {
                case UrlType.utBaidu:
                    urlType = UrlType.ut168;
                    webResult.Navigate(url168);
                    break;
                case UrlType.ut168:
                    urlType = UrlType.utPk10;
                    webResult.Navigate(urlPk10);
                    break;
                case UrlType.utPk10:
                    urlType = UrlType.utNone;
                    break;
                case UrlType.utNone:
                    urlType = UrlType.utBaidu;
                    webResult.Navigate(urlBaidu);
                    break;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            string text = string.Empty;

            Count++;
            if (Count == 3)
            {
                Count = 0;
                resultPeroid = string.Empty;
                resultValue = string.Empty;
                switch (urlType)
                {
                    case UrlType.utBaidu:
                        GetBaiDuResult(webResult, ref resultPeroid, ref resultValue);
                        text = urlBaidu;
                        break;
                    case UrlType.ut168:
                        Get168Result2(webResult, ref resultPeroid, ref resultValue);
                        text = url168;
                        break;
                    case UrlType.utPk10:
                        GetPK10Result(webResult, ref resultPeroid, ref resultValue);
                        text = urlPk10;
                        break;
                    case UrlType.utNone:
                        break;
                    default:
                        break;
                }
                timer2.Enabled = false;

                //如果获取的数据有一个为空，则不管
                if (string.IsNullOrEmpty(resultPeroid) || string.IsNullOrEmpty(resultValue))
                    return;

                if (siteCount < totalSites)
                {
                    LogText2(text);
                    LogText(resultPeroid, resultValue);
                }
                siteCount++;
                if (siteCount == totalSites)
                    siteCount = 0;
                //第一次运行时写入数据
                if (string.Compare(resultPeroid, oldPeroid) > 0)
                {
                    oldPeroid = resultPeroid;
                    BetXml.WriteResult(resultPeroid, resultValue);
                }
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

    }

    public enum UrlType
    {
        utBaidu = 0,
        ut168 = 1,
        utPk10 = 2,
        utNone = 10,
    }
}
